//
//  RegisterViewController.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "RegisterViewController.h"
#import "SetBlack.h"
#import "BlackManager.h"
@interface RegisterViewController ()
@property (nonatomic, retain)UITextField *username;
@property (nonatomic, retain)UITextField *pwd;
@property (nonatomic, retain)UITextField *repwd;
@end
@implementation RegisterViewController
-(void)dealloc
{
    [_username release];
    [_pwd release];
    [_repwd release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title  = @"注册";
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-back"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)] autorelease];
    self.username = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 10, 100, self.view.frame.size.width / 10 * 8, 30)];
    _username.placeholder = @"请输入用户名";
    _username.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_username];
    [_username release];
    
    self.pwd = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 10, _username.frame.origin.y + _username.frame.size.height + 30, self.view.frame.size.width / 10 * 8, 30)];
    _pwd.placeholder = @"请输入密码";
    _pwd.secureTextEntry = YES;
    _pwd.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_pwd];
    [_pwd release];
    
    self.repwd = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 10, _pwd.frame.origin.y + _pwd.frame.size.height + 30, self.view.frame.size.width / 10 * 8, 30)];
    _repwd.placeholder = @"请确认密码";
    _repwd.secureTextEntry = YES;
    _repwd.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_repwd];
    [_repwd release];
    
    //注册按钮
    UIButton *registerLogin = [UIButton buttonWithType:UIButtonTypeSystem];
    registerLogin.frame = CGRectMake(_repwd.frame.origin.x, _repwd.frame.origin.y + _repwd.frame.size.height + 30, self.view.frame.size.width / 4, 30);
    CGRect f =registerLogin.frame;
    [registerLogin setBackgroundColor:[UIColor cyanColor]];
    [registerLogin setTitle:@"注册" forState:UIControlStateNormal];
    [registerLogin addTarget:self action:@selector(registerAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerLogin];
    
    //取消按钮
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    f.origin.x = self.view.frame.size.width / 5 * 3;
    cancleBtn.frame = f;
    [cancleBtn setBackgroundColor:[UIColor orangeColor]];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancleBtn];
    
}
#pragma mark - 点击屏幕收起键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
#pragma mark - 夜间模式设置
//设置夜间模式  重写BaseViewController中的方法
-(void)setNightDayModel
{
    [SetBlack setBackgroundColorWithView:self.view];
}
-(void)setLightDayModel
{
    [SetBlack setBackgroundColorWithView:self.view];
}

-(void)registerAction
{
    if (_username.text.length != 0 && [_pwd.text isEqualToString: _repwd.text] && _pwd.text.length != 0 ) {
        //把姓名写在单例里
        [[NSUserDefaults standardUserDefaults]setObject:_username.text forKey:@"name"];
        //弹窗
        UIAlertController *promptAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:promptAlert animated:YES completion:^{
            
        }];
        #pragma mark - 存储数据
        //在Caches中创建文件储存用户的姓名和密码
        //存储姓名
        NSString *namePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"name.a"];
        //从namepath路径解档获取数组
        NSMutableArray *nameArr = [NSKeyedUnarchiver unarchiveObjectWithFile:namePath];
        if (nameArr) {
            [nameArr addObject:_username.text];
        }else{
            nameArr = [NSMutableArray array];
            [nameArr addObject:_username.text];
        }
        
        //将数组归档
        [NSKeyedArchiver archiveRootObject:nameArr toFile:namePath];
        //存储密码
        NSString *pwdPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"pwd.a"];
        NSMutableArray *pwdArr = [NSKeyedUnarchiver unarchiveObjectWithFile:pwdPath];
        if (pwdArr) {
            [pwdArr addObject:_pwd.text];
        }else{
            pwdArr = [NSMutableArray array];
            [pwdArr addObject:_pwd.text];
        }
        //密码归档
        [NSKeyedArchiver archiveRootObject:pwdArr toFile:pwdPath];
        //储存照片
        //存储照片的本地路径
        NSString *picturePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"picture.a"];
        NSMutableArray *pictureArr = [NSKeyedUnarchiver unarchiveObjectWithFile:picturePath];
        if (pictureArr) {
            [pictureArr addObject:@"1"];
        }else{
            pictureArr = [NSMutableArray array];
            [pictureArr addObject:@"1"];
        }
        //图片归档
        [NSKeyedArchiver archiveRootObject:pictureArr toFile:picturePath];
        
        //存储新创建用户的位置(NSNumber型存放)
        NSNumber *indexNum = @(pictureArr.count - 1);
        [[NSUserDefaults standardUserDefaults]setObject:indexNum forKey:@"index"];
       
        //NSUserDefaults 设置登陆成功状态
        NSLog(@"%@", NSHomeDirectory());
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"register"];
        [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"OtherLogin"];
        //让弹窗消失
        [self dismissViewControllerAnimated:YES completion:^{
            //
        [self.navigationController popToRootViewControllerAnimated:YES];
            NSLog(@"注册成功");
        }];
        
    }
}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
